import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.running;

import java.util.ArrayList;
import java.util.List;

import models.Cart;
import models.CartItem;
import models.Transaction.Type;

import org.junit.Test;

import prefix.duydo.repository.JpaRepository;
import prefix.payment.PaymentDueProcessor;
import prefix.payment.PaymentExeProcessor;
import prefix.payment.PaymentNotifyProcessor;
import prefix.payment.PaymentPullProcessor;
import prefix.transaction.BasicTransaction;
import prefix.transaction.TransactionTest;
import processor.CartProcessor;
import processor.LoanProcessor;
import processor.OrderProcessor;
import repository.LoanAppRepository;
import repository.LoanRepository;
import service.CartService;
import service.LoanAppService;
import service.LoanService;
import service.exception.CartException;
import service.exception.LoanAppException;
import service.exception.OrderException;
import service.impl.CartServiceImpl;

public class Invest_issue_pay_flow {

    @Test
    public void startTest() {
        running(fakeApplication(), new Runnable() {
            @Override
            public void run() {
                flow();
            }
        });
    }

    public void flow() {
        try {
//                        createCart(4000, 6000, 10018, 10019, 10021);
//                        createCart(6000, 4000, 10018, 10019, 10022);
//                        submitOrder(10010);
//                        submitOrder(10011);
            //                                    approveLoan(10018);
            //                                    approveLoan(10019);
//                        issueLoan();
//            executeIssueTransaction(10018);
//                        executeT3(10129);
                                    paymentFlow();
//                        executePayment();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void createCart(double amount1, double amount2, Integer loanId1, Integer loanId2, Integer aid)
            throws CartException {
        CartService cart = new CartServiceImpl();
        CartProcessor cartProcessor = cart.getProcessor();
        List<CartItem> items = new ArrayList<CartItem>();
        CartItem item1 = new CartItem(amount1, loanId1, null);
        CartItem item2 = new CartItem(amount2, loanId2, null);
        items.add(item1);
        items.add(item2);
        cartProcessor.createCart(items, aid, amount1 + amount2);
    }

    private void submitOrder(Integer cartId) throws OrderException {
        OrderProcessor processor = new OrderProcessor();
        Cart cart = new JpaRepository().find(Cart.class, cartId);
        CartService cartService = new CartServiceImpl(cart);
        processor.submitOrder(cartService);
    }

    private void approveLoan(Integer loanId) throws LoanAppException {
        LoanAppService appService = LoanAppRepository.getLoanAppById(loanId);
        appService.getProcessor().approveLoanApp();
    }

    private void issueLoan() {
        LoanProcessor.issueLoans();
    }

    private void executeIssueTransaction(Integer loanId) {
        LoanService loanService = LoanRepository.getLoanById(loanId);
        loanService.getProcessor().executeIssueTransaction();
    }

    private void executeT3(Integer transId) {
        BasicTransaction.executeNow(transId);
    }

    private void processNotify() {
        PaymentNotifyProcessor.processTodayNotification();
    }

    private void processPull() {
        PaymentPullProcessor.processPull();
    }

    private void processPayment() {
        PaymentDueProcessor.processPayment();
    }

    private void paymentFlow() {
        processNotify();
        processPull();
        processPayment();
    }

    private void executePayment() {
        PaymentExeProcessor.executePaymentTrans();
    }

    private void borrowerAddMoney(Type type, Integer initialAid, Integer fromAid, Integer toAid, Integer fromAccount,
            Integer toAccount, double amount) {
        TransactionTest.transTest(type, initialAid, fromAid, toAid, fromAccount, toAccount, amount);
    }

}
